/*
 * @LastEditors: 杜康
 * @LastEditTime: 2021-10-01 16:10:06
 */
// 队列
class Queue {
    constructor () {
        this.count = 0
        this.lowestCount = 0
        this.items = {}
    }
    enqueue (elements) {
        this.items[this.count] = elements
        this.count += 1
    }
    dequeue () {
        if (this.isEmpty()) {
            return undefined
        }
        const result = this.items[this.lowestCount]
        delete this.items[this.lowestCount]
        this.lowestCount += 1
        return result
    }
    peek () {
        if (this.isEmpty()) {
            return undefined
        }
        return this.items[this.lowestCount]
    }
    isEmpty () {
        return this.count - this.lowestCount === 0
    }
    size () {
        return this.count - this.lowestCount
    }
    clear () {
        this.items = {}
        this.lowestCount = 0
        this.count = 0
    }
    toString () {
        if (this.isEmpty()) {
            return ''
        }
        let objString = `${this.items[this.lowestCount]}`
        for (let i = this.lowestCount + 1; i < this.count; i++) {
            objString = `${objString},${this.items[i]}`
        }
        return objString
    }
}

const queue = new Queue()
console.log('🚀 ~ queue.isEmpty()', queue.isEmpty())
queue.enqueue('Jhon')
queue.enqueue('Jack')
console.log('🚀 ~ queue', queue)
queue.dequeue()
console.log('🚀 ~ queue', queue)
queue.enqueue('Yuki')
console.log('🚀 ~ queue.toString()', queue.toString())
queue.dequeue()
console.log('🚀 ~ queue', queue)